
Ext.namespace("Ext.milan.pagejs.page_zdxzdc");
Ext.milan.pagejs.page_zdxzdc.userWin = null;
Ext.milan.pagejs.page_zdxzdc.subWin = null;
Ext.milan.pagejs.page_zdxzdc.REQUEST_ACTION = "zdxzdc";
var selectPostionTree = null;
Ext.milan.pagejs.page_zdxzdc.docLoadBeforeCallBack = function() {
};

Ext.milan.pagejs.page_zdxzdc.docOnReadyCallback = function() {

};

/**
 * 关闭子弹出窗口
 */
Ext.milan.pagejs.page_zdxzdc.closeSubWin = function() {
	Milan.commonutils.closeModelPop(Ext.milan.pagejs.page_zdxzdc.subWin);
};

/**
 * 关闭弹出窗口
 */
Ext.milan.pagejs.page_zdxzdc.closeWin = function(winName) {
	if(winName){
		if(winName=='inactivateConsignerWin')
			inactivateConsignerWin.closeDialog();
	}
	else
		Milan.commonutils.closeModelPop(Ext.milan.pagejs.page_zdxzdc.userWin);
};

/*------------------------ private methods -----------------------*/

/**
 * 查询所有
 */
Ext.milan.pagejs.page_zdxzdc._search = function() {
	Ext.milan.pagejs.common_utils.query(false, "zdxzdc/list.do");
};

Ext.milan.pagejs.page_zdxzdc._formCheck = function(frm) {
	function focusElement(el) {
		if(el.focus)
			el.focus();
	};
	return true;
};

/**
 * 保存信息
 */
Ext.milan.pagejs.page_zdxzdc._saveInfo = function(formId) {
	if(!Ext.milan.pagejs.page_zdxzdc._formCheck($(formId)))
		return;
		
	/*
	var selectedUsers = $("selected_users_in_group");
	for(var i = 0; i < selectedUsers.length; i++) {
		selectedUsers[i].selected = true;
	}
	*/
		
	Milan.ajaxutility.formSubmit({
		form:formId,
		success:function(response)
		{
			Ext.milan.pagejs.page_zdxzdc.closeWin();
			Ext.milan.pagejs.page_zdxzdc.query();
		}
	});
};

/**
 * 条件查询列表
 */
Ext.milan.pagejs.page_zdxzdc.query = function() {
	Milan.pagejs.page_index.updateCurrentTabByForm("query_zdxzdc_list_form");
};

Ext.milan.pagejs.page_zdxzdc.jldc = function(formId) {
	Milan.ajaxutility.downloadFile({form: $(formId) });
};

/**
 * 显示新建表单
 */
Ext.milan.pagejs.page_zdxzdc.showAddForm = function() {
	var url = Ext.milan.pagejs.page_zdxzdc.REQUEST_ACTION + "/create.do";
	Ext.milan.pagejs.page_zdxzdc.userWin = Milan.commonutils.createModelPop({
		title:"新建", 
		url:url,
		width:650
	});
};

/**
 * 显示编辑表单
 * 
 * @param ID
 */
Ext.milan.pagejs.page_zdxzdc.showEditForm = function(id) {
	var url = Ext.milan.pagejs.page_zdxzdc.REQUEST_ACTION + "/edit.do?id=" + id;
	Ext.milan.pagejs.page_zdxzdc.userWin = Milan.commonutils.createModelPop({
		title:"编辑",
		url:url,
		width:650
	});
};

/**
 * 保存新建用户信息
 */
Ext.milan.pagejs.page_zdxzdc.save = function(formId) {
	Ext.milan.pagejs.page_zdxzdc._saveInfo(formId);
};

/**
 * 保存编辑后的信息
 */
Ext.milan.pagejs.page_zdxzdc.edit = function(formId) {
	Ext.milan.pagejs.page_zdxzdc._saveInfo(formId);
};

Ext.milan.pagejs.page_zdxzdc.deleteForm = function(id) {
	var url = Ext.milan.pagejs.page_zdxzdc.REQUEST_ACTION + "/delete.do?id=" + id;
	new Milan.widget.ConfirmDialog("确认信息", "确认要删除该信息吗？", {
		confirmCallback: function() {
			Milan.ajaxutility.formSubmit({
				url: url,
				method: 'post',
				success: function(response, option)
				{
					Ext.milan.pagejs.page_zdxzdc.query();
				}
			});
		}
	});	
};

Ext.milan.pagejs.page_zdxzdc.selectUsers = function(ele,str) {

	new Milan.widget.Selector({
		orgType:"12",
		title:"选择人员",
		deptUrl:"dept/selectDept.do?",
		quickSelectUrl:"user/quickUser.do?",
		searchUrl:"user/searchUser.do?",
		isRecentEnable :false,
		isUpdateRecentEnable :false,
		isDeptSelEnable:true
	},
	true,
	{
		onSelectedCallBack : function(arr)
		{
			var formEle = ele.form;
			var obj = eval("formEle."+str);
			obj.value=arr[0].id;
			obj = eval("formEle."+"select"+str);
			obj.value=arr[0].name;
		}
	});
};

/**
 * 查看详情
 * 
 * @param entityId 用户ID
 */
Ext.milan.pagejs.page_zdxzdc.viewDetail = function(entityId) {
	var url = Ext.milan.pagejs.page_zdxzdc.REQUEST_ACTION + "/show.do?id=" + entityId;
	Ext.milan.pagejs.page_zdxzdc.userWin = Milan.commonutils.createModelPop({
		title:"详细信息",
		url:url,
		width:700
	});
};


/**
 * 选择用户
 * 
 * @param chkObj 复选框
 * @param realName 用户姓名
 */
Ext.milan.pagejs.page_zdxzdc.selectUser = function(chkObj) {
	var userContaniner = $("selected_users_in_group");
	if(chkObj.checked) {
		if($("privilegegroup_user_" + chkObj.value)) return;
		var optionEl = document.createElement("option");
			optionEl.id = "privilegegroup_user_" + chkObj.value;
			optionEl.value = chkObj.value;
			optionEl.text = chkObj.actualName;
		userContaniner.add(optionEl);
			
		//显示“移除”按钮
		if($("del_user_in_group_btn").style.display == "none") {
			Element.show('del_user_in_group_btn');
		}	
	}else {
		userContaniner.remove($('privilegegroup_user_' + chkObj.value).index);

		//隐藏“移除”按钮
		if(userContaniner.length == 0) {
			Element.hide('del_user_in_group_btn');
		}
	}
};

/**
 * 选择全部用户
 * 
 * @param checkState 具有全选功能的复选框的选中状态
 */
Ext.milan.pagejs.page_zdxzdc.selectAllUsers = function(checkState) {
	var refChkItems = document.getElementsByName("userChk");
	CheckboxSelectUtils.selectAll(checkState, refChkItems, Ext.milan.pagejs.page_zdxzdc.selectUser);
};

/**
 * 显示“选择人员”弹出框
 */
Ext.milan.pagejs.page_zdxzdc.selectUsers = function(selectItemId) {

	new Milan.widget.Selector({
		orgType:"12",
		title:"选择人员",
		deptUrl:"dept/selectDept.do?",
		quickSelectUrl:"user/quickUser.do?",
		searchUrl:"user/searchUser.do?",
		isRecentEnable :false,
		isUpdateRecentEnable :false,
		isDeptSelEnable:true
	},
	true,
	{
		onSelectedCallBack : function(arr)
		{
			var selectItem = $(selectItemId);
			var isExisted = false;
			if(arr != null && arr.length > 0) {
				for (var i = 0; i<arr.length; i++) {
					for (var x = 0; x < selectItem.length; x++) {
						if (arr[i].id == selectItem[x].value) {
							isExisted=true;
							break;
						}	
					}
					
					if (!isExisted) {
						var optionEl = document.createElement("OPTION");
						optionEl.text = arr[i].name;
						optionEl.value = arr[i].id;
						selectItem.options.add(optionEl);
					}
						
					isExisted = false;
				}
			}  
			
			Ext.milan.pagejs.page_zdxzdc.displayBtn(selectItem);
		}
	});
};

Ext.milan.pagejs.page_zdxzdc.displayBtn = function(selectItem) {
	if(selectItem.length > 0) {
		Element.show('del_user_in_group_btn');
	}else {
		Element.hide('del_user_in_group_btn');
	}
}

/**
 * 移除用户
 */
Ext.milan.pagejs.page_zdxzdc.removeUser = function() {
	var selectedUsers = $("selected_users_in_group");
	if(selectedUsers.length == 0)
		return;
				
	var delUserArray = [];
	for(var i = 0; i < selectedUsers.length; i++) {
		if(selectedUsers[i].selected) {
			delUserArray.push(selectedUsers[i]);
		}
	}
	
	if(selectedUsers.selectedIndex ==-1){
		new Milan.widget.Dialog("信息提示","请选择要移除的人员!");
		return;
	}
			
	for(var j = 0; j < delUserArray.length; j++) {
		selectedUsers.removeChild(delUserArray[j]);
	}
	
	if(selectedUsers.length == 0) 
		Element.hide('del_user_in_group_btn');
};

